package obs;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ManyThreadV2 {
    public static void debug(Object o){
        String s = ""+Thread.currentThread().getName();

        System.out.println(s+" thread: "+o.toString());
    }

//    public static List<MeiMeiEvent> meiMeiqueue= new ArrayList<>();
    public static List<MeiMeiEvent> meiMeiqueueToMing= new ArrayList<>();
    public static ReentrantLock meimeiMinglock = new ReentrantLock();
    public static Condition meimeiMingtag = meimeiMinglock.newCondition();

    public static List<MeiMeiEvent> meiMeiqueueToZhang= new ArrayList<>();
    public static ReentrantLock meimeiZhanglock = new ReentrantLock();
    public static Condition meimeiZhangtag = meimeiZhanglock.newCondition();
    public static SyncXiaoMing syncXiaoMing = new SyncXiaoMing();
    public static SyncXiaoZhang syncXiaoZhang = new SyncXiaoZhang();

    public static void main(String[] args) throws InterruptedException {

        new Thread(()-> {
            SyncMeiMei syncMeiMei = new SyncMeiMei();
            Scanner scanner = new Scanner(System.in);
            while (true){
                String s = scanner.nextLine();
                Integer r= 0;
                try {
                    r = Integer.parseInt(s);
                }catch (Exception e){
                    r=100;
                }
                syncMeiMei.setAge(r);
                debug("妹妹年龄　"+syncMeiMei.getAge());
                MeiMeiEvent meiMeiEvent = new MeiMeiEvent(r, System.currentTimeMillis());
                meimeiMinglock.lock();
                try{
                    // 事件
                    meiMeiqueueToMing.add(meiMeiEvent);
                    //通知notify
                    meimeiMingtag.signal();
                }finally {
                    meimeiMinglock.unlock();
                }
                meimeiZhanglock.lock();
                try{
                    // 事件
                    meiMeiqueueToZhang.add(meiMeiEvent);
                    //通知notify
                    meimeiZhangtag.signal();
                }finally {
                    meimeiZhanglock.unlock();
                }
            }


        },"meimei").start();
        new Thread(()-> {
            while (true){
                MeiMeiEvent event = null;
                meimeiMinglock.lock();
                try{
                    while (meiMeiqueueToMing.size()==0){

                        meimeiMingtag.await();
                    }
                    //回调函数
                    //xiaoming_age=meimei_age+8;
                    event= meiMeiqueueToMing.remove(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    meimeiMinglock.unlock();
                }
                updateXiaoming(event);
                debug("小明年龄　"+syncXiaoMing.getAge());
            }


        },"xiaoming").start();
        new Thread(()-> {
            while (true){
                MeiMeiEvent event = null;
                meimeiZhanglock.lock();
                try{
                    while (meiMeiqueueToZhang.size()==0){

                        meimeiZhangtag.await();
                    }
                    //回调函数
                    //xiaoming_age=meimei_age+8;
                    event= meiMeiqueueToZhang.remove(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    meimeiZhanglock.unlock();
                }
                updateXiaozhang(event);
                debug("小张年龄　"+syncXiaoZhang.getAge());
            }


        },"xiaozhang").start();
        while (true){
            Thread.sleep(2000);
            System.out.println();
            meimeiMinglock.lock();
            try{
                debug("妹妹－＞小明queue　："+meiMeiqueueToMing);
            }finally {
                meimeiMinglock.unlock();
            }
            meimeiZhanglock.lock();
            try{
                debug("妹妹－＞小张queue　："+meiMeiqueueToZhang);
            }finally {
                meimeiZhanglock.unlock();
            }
        }

//        System.out.println("end");
    }
    public static void updateXiaozhang(MeiMeiEvent meimei){
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        syncXiaoZhang.setAge(meimei.getAge()+6);
    }
    public static void updateXiaoming(MeiMeiEvent meimei){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        syncXiaoMing.setAge(meimei.getAge()+8);
    }
}

